package com.sixth.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.RoleDao;
import com.sixth.entity.Role;
import com.sixth.service.RoleService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project01
 * @description:
 * @author: 高振杨
 * @create: 2021-12-17 19:27
 **/
@Service(value = "roleservice")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    public CommonResult SelectAll() {
        List<Role> roles= roleDao.selectAll();
        return new CommonResult(2000,"查询成功",roles);
    }

    public TableLayUi findRoleByPage(int page , int limit) {
        PageHelper.startPage(page, limit);
        List<Role> roles = roleDao.findAll();
        PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
        return new TableLayUi(0,"",pageInfo.getTotal(),pageInfo.getList());
    }

    public CommonResult addOrUpdate(Role role) {
        Integer i;
        if (role.getRoleid() != null) {
            i = roleDao.updateByPrimaryKeySelective(role);
        } else {
            i = roleDao.insertSelective(role);
        }
        CommonResult result;
        if (i > 0) {
            result = CommonResult.success((Object) null, "操作成功");
        } else {
            result = CommonResult.fail("操作失败");
        }
        return result;
    }

    public  CommonResult delById(Integer roleid){
        Integer i = this.roleDao.deleteByPrimaryKey(roleid);
        CommonResult result;
        if (i > 0) {
            result = CommonResult.success((Object)null, "删除成功");
        } else {
            result = CommonResult.fail("删除失败");
        }

        return result;
    }

    public  CommonResult getOneRole(String rolename){
        List<Role> oneRole =roleDao.getOneRole(rolename);
        return  oneRole.size() >0 ? CommonResult.fail("角色名重复"):CommonResult.success();
    }
}
